/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import entity.Jobs;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author owtee
 */
@Stateless
public class JobsFacade extends AbstractFacade<Jobs> {

    @PersistenceContext(unitName = "MSGPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public JobsFacade() {
        super(Jobs.class);
    }

    public List<Jobs> findReqAll(boolean activeFlag,
            String userId,
            List<String> statusId,
            String jobId,
            Date requestDateSt,
            Date requestDateEn,
            Date expectDateSt,
            Date expectDateEn) {
        em.getEntityManagerFactory().getCache().evictAll();
        return em.createQuery("SELECT j FROM Jobs j WHERE j.activeFlag = :activeFlag AND j.requestUser.userId = :userId AND j.statusId.statusId IN :statusId AND j.jobId LIKE :jobId AND (j.requestDate BETWEEN :requestDateSt AND :requestDateEn) AND (j.expectDate BETWEEN :expectDateSt AND :expectDateEn)")
                .setParameter("activeFlag", activeFlag)
                .setParameter("userId", userId)
                .setParameter("statusId", statusId)
                .setParameter("jobId", jobId)
                .setParameter("requestDateSt", requestDateSt)
                .setParameter("requestDateEn", requestDateEn)
                .setParameter("expectDateSt", expectDateSt)
                .setParameter("expectDateEn", expectDateEn)
                .getResultList();
    }

    public List<Jobs> findReqAll(int start,
            int end,
            boolean activeFlag,
            String userId,
            List<String> statusId,
            String jobId,
            Date requestDateSt,
            Date requestDateEn,
            Date expectDateSt,
            Date expectDateEn) {
        em.getEntityManagerFactory().getCache().evictAll();
        return em.createQuery("SELECT j FROM Jobs j WHERE j.activeFlag = :activeFlag AND j.requestUser.userId = :userId AND j.statusId.statusId IN :statusId AND j.jobId LIKE :jobId AND (j.requestDate BETWEEN :requestDateSt AND :requestDateEn) AND (j.expectDate BETWEEN :expectDateSt AND :expectDateEn)")
                .setFirstResult(start)
                .setMaxResults(end)
                .setParameter("activeFlag", activeFlag)
                .setParameter("userId", userId)
                .setParameter("statusId", statusId)
                .setParameter("jobId", jobId)
                .setParameter("requestDateSt", requestDateSt)
                .setParameter("requestDateEn", requestDateEn)
                .setParameter("expectDateSt", expectDateSt)
                .setParameter("expectDateEn", expectDateEn)
                .getResultList();
    }

    public List<Jobs> findMsgAll(boolean activeFlag,
            List<String> statusId,
            String jobId,
            Date requestDateSt,
            Date requestDateEn,
            Date expectDateSt,
            Date expectDateEn) {
        em.getEntityManagerFactory().getCache().evictAll();
        return em.createQuery("SELECT j FROM Jobs j WHERE j.activeFlag = :activeFlag AND j.statusId.statusId IN :statusId AND j.jobId LIKE :jobId AND (j.requestDate BETWEEN :requestDateSt AND :requestDateEn) AND (j.expectDate BETWEEN :expectDateSt AND :expectDateEn)")
                .setParameter("activeFlag", activeFlag)
                .setParameter("statusId", statusId)
                .setParameter("jobId", jobId)
                .setParameter("requestDateSt", requestDateSt)
                .setParameter("requestDateEn", requestDateEn)
                .setParameter("expectDateSt", expectDateSt)
                .setParameter("expectDateEn", expectDateEn)
                .getResultList();
    }

    public List<Jobs> findMsgAll(int start,
            int end,
            boolean activeFlag,
            List<String> statusId,
            String jobId,
            Date requestDateSt,
            Date requestDateEn,
            Date expectDateSt,
            Date expectDateEn) {
        em.getEntityManagerFactory().getCache().evictAll();
        return em.createQuery("SELECT j FROM Jobs j WHERE j.activeFlag = :activeFlag AND j.statusId.statusId IN :statusId AND j.jobId LIKE :jobId AND (j.requestDate BETWEEN :requestDateSt AND :requestDateEn) AND (j.expectDate BETWEEN :expectDateSt AND :expectDateEn)")
                .setFirstResult(start)
                .setMaxResults(end)
                .setParameter("activeFlag", activeFlag)
                .setParameter("statusId", statusId)
                .setParameter("jobId", jobId)
                .setParameter("requestDateSt", requestDateSt)
                .setParameter("requestDateEn", requestDateEn)
                .setParameter("expectDateSt", expectDateSt)
                .setParameter("expectDateEn", expectDateEn)
                .getResultList();
    }

    public List<Jobs> findMsgFinishAll(boolean activeFlag,
            String userId,
            List<String> statusId,
            String jobId,
            Date expectDateSt,
            Date expectDateEn) {
        em.getEntityManagerFactory().getCache().evictAll();
        return em.createQuery("SELECT j FROM Jobs j WHERE j.activeFlag = :activeFlag AND j.msgUser.userId = :userId AND j.statusId.statusId IN :statusId AND j.jobId LIKE :jobId AND (j.expectDate BETWEEN :expectDateSt AND :expectDateEn)")
                .setParameter("activeFlag", activeFlag)
                .setParameter("userId", userId)
                .setParameter("statusId", statusId)
                .setParameter("jobId", jobId)
                .setParameter("expectDateSt", expectDateSt)
                .setParameter("expectDateEn", expectDateEn)
                .getResultList();
    }

    public List<Jobs> findMsgFinishAll(int start,
            int end,
            boolean activeFlag,
            String userId,
            List<String> statusId,
            String jobId,
            Date expectDateSt,
            Date expectDateEn) {
        em.getEntityManagerFactory().getCache().evictAll();
        return em.createQuery("SELECT j FROM Jobs j WHERE j.activeFlag = :activeFlag AND j.msgUser.userId = :userId AND j.statusId.statusId IN :statusId AND j.jobId LIKE :jobId AND (j.expectDate BETWEEN :expectDateSt AND :expectDateEn)")
                .setFirstResult(start)
                .setMaxResults(end)
                .setParameter("activeFlag", activeFlag)
                .setParameter("userId", userId)
                .setParameter("statusId", statusId)
                .setParameter("jobId", jobId)
                .setParameter("expectDateSt", expectDateSt)
                .setParameter("expectDateEn", expectDateEn)
                .getResultList();
    }

    public List<Jobs> findNotCloseJob(Date expectDate) {
        em.getEntityManagerFactory().getCache().evictAll();
        return em.createQuery("SELECT j FROM Jobs j WHERE j.expectDate = :expectDate AND j.finishDate IS NULL")
                .setParameter("expectDate", expectDate)
                .getResultList();
    }

    public List<Jobs> findCloseJob(Date expectDate) {
        em.getEntityManagerFactory().getCache().evictAll();
        return em.createQuery("SELECT j FROM Jobs j WHERE j.expectDate = :expectDate AND j.finishDate IS NOT NULL")
                .setParameter("expectDate", expectDate)
                .getResultList();
    }
}
